﻿//功能描述：系统设置-人员管理
using Hong.IRepository.IRepository_SystemSettings;
using Hong.Model.Dto.PesponseDto.ResponseModel.ResultModel;
using Hong.Model.SysDomain.Model_SystemSettings;
using Hong.Model.SysDomain.Model_SystemSettings.ViewModel;
using Hong.Repository.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hong.Repository.Repository.Repository_SystemSettings
{
    public class PersonnelDal : DataBase, IPersonnelDal
    {
        #region 构造器注入

        private readonly T_HongShi_3Context _t_HongShi_3Context;
        private readonly DapperHelper _dapperHelper;


        public PersonnelDal(T_HongShi_3Context t_HongShi_3Context, DapperHelper dapperHelper) : base(t_HongShi_3Context)
        {
            _t_HongShi_3Context = t_HongShi_3Context;
            _dapperHelper= dapperHelper;
        }

        #endregion

        #region 人员显示
        public async Task<PageResponse<PersonnelModel>> GetPer(int? depId, string PersonnelName,int pageindex,int pagesize)
        {
            var sql = "select * from peop where 1=1";
            if (depId!=null)
            {
                sql += $" and f.depId={depId}";
            }
            if (PersonnelName!=null)
            {
                sql += $" and a.PersonnelName like '%{PersonnelName}%'";
            }
            var list= await _dapperHelper.QueryAsync<PersonnelModel>(sql);
            var rlist = list.OrderByDescending(m => m.PersonneISort).Skip((pageindex - 1) * pagesize).Take(pagesize).ToList();
            PageResponse<PersonnelModel> q = new PageResponse<PersonnelModel>(rlist, pageindex, list.Count());
            return q;
        }

        #endregion

        #region 人员添加
        public async Task<ResponseModel<bool>> AddPer(Personnel personnel)
        {
            try
            {
                var result = new ResponseModel<bool>();
                int count = 1;
                personnel.PersonnelId = Guid.NewGuid();

                await AddAsync(personnel);
                count = await SaveChangesAsync();
                return count > 0 ? result.Succeed(true) : result.Fail("操作失败");
            }
            catch (Exception ex)
            {

                throw;
            }
            
        }
        #endregion

        #region 人员删除
        public async Task<ResponseModel<bool>> DelPer(Guid PersonnelId)
        {
            var result = new ResponseModel<bool>();
            int count;

            await DeleteAsync<Personnel>(PersonnelId);
            count = await SaveChangesAsync();
            return count > 0 ? result.Succeed(true) : result.Fail("操作失败");
        }
        #endregion
    }
}
